<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>注册</title>
    <style>
    #box{
        display: none;        
    }
    </style>
</head>

<body>
    <h1>验证用户名是否存在并在用户名下产生单选按钮跟着的未定义的名字</h1>

    用户名：<input type="text" id="username" name="username" onblur="checkUser()" /><span id="userMsg"></span><br />
    <ul id="box">
        <li>
            <input type="radio"><span></span>
        </li>
        <li>
            <input type="radio"><span></span>
        </li>
        <li>
            <input type="radio"><span></span>
        </li>
    </ul>
</body>
</html>

<script>
    function checkUser() {
        let xhr = new XMLHttpRequest();
        xhr.open("get", "checkuser.php?username=" + getId("username").value, true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                if (xhr.responseText == 1) {
                    getId("userMsg").innerHTML = "用户名已经使用";
                } else {
                    getId("userMsg").innerHTML = "用户名未使用"; 
									   
                    let boxDom = getId("box");
                    for(let i=0;i<boxDom.children.length;i++){
                        boxDom.children[i].lastElementChild.innerHTML = getId("username").value+parseInt(Math.random()*5);
						//
						boxDom.children[i].firstElementChild.setAttribute("data-index",i);
						//console.log(boxDom.children[parseInt("data-index")].lastElementChild.innerHTML)
						// if(){
						// 	getId("username").value=boxDom.children[parseInt("data-index")].lastElementChild.innerHTML;
							
						// }
                    }
                    boxDom.style.display = "block";
                }
            }
        }
        xhr.send();
    }
    function getId(str){
        return document.getElementById(str);
    }
</script>

<!-- 点击按钮 随机生成的用户名上去 -->